/*
 * @Author: your name
 * @Date: 2021-10-09 10:03:45
 * @LastEditTime: 2021-11-25 15:26:02
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \cpp11\threadpool.h
 */
#pragma once
#include <thread>
#include <mutex>
#include <atomic>
#include <vector>
#include <queue>
#include <condition_variable>
#include <iostream>
#include <functional>
#include <utility>
#include "threadpool.h"

extern "C"{
    #if !defined (_WIN32) && !defined (_WIN64)
    #define LINUX
    #include <sysconf.h>
    #else
    #define WINDOWS
    #include <windows.h>
    #endif
    unsigned int CoreCount();
}

namespace general{
    class CThreadPool;
    enum  PRIORITY  {
        MIN = 1, NORMAL = 25, MAX = 50
    };

    class Task{
        public:
            Task() {
            }
            void SetPriority(int priority) {
                if (priority>(PRIORITY::MAX))
                {
                    priority = (PRIORITY::MAX);
                }
                else if (priority>(PRIORITY::MAX))
                {
                    priority = (PRIORITY::MIN);
                }
            }
            virtual void Run() = 0;
            virtual void OnDone() = 0;
        protected:
            int priority_;
    };

    class Worker{
    public:
        // The required constructor for this example.
        explicit Worker(int& evenCount)
            : m_evenCount(evenCount) { }

        void operator()(CThreadPool *pool) const {

        }
    private:
    // Default assignment operator to silence warning C4512.
        Worker& operator=(const Worker&);
        int& m_evenCount;
    };

    typedef class CThreadPool {
    public:
        CThreadPool(int num);
        ~CThreadPool();
        int AddTask(Task *);
        void Start();
        void Stop();
        bool Started();
        void StopAll();

    private:
        CThreadPool();
        void Process(int id);
        uint32_t mThreadCnt;
        bool mStarted;
        bool mStoping;
        Task *PopTask();
        std::vector<std::thread*> mThreads;
        std::queue<Task *> mTasks;
        std::mutex mMutex;
        std::condition_variable mQueueAvaliableCondition;
    }ThreadPool;
}

